﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;


namespace WindowsFormsApplication1
{
    public class ircClient
    {
        NetworkStream stream;
        int bytesRead;
        string incomingText;
        string readText;
        StreamWriter writer;
        StreamReader reader;
        TcpClient tcpClient;
        private Thread listener;
        public ircClient()
        {
            readText = "";
            incomingText = "";
        }

        public void connect(string server, int port)
        {
            try
            {
                tcpClient = new TcpClient(server, port);
                stream = tcpClient.GetStream();
                writer = new StreamWriter(stream);
                reader = new StreamReader(stream);
                byte[] data = new byte[tcpClient.ReceiveBufferSize];
                bytesRead = stream.Read(data, 0, System.Convert.ToInt32(tcpClient.ReceiveBufferSize));

                writer.WriteLine("NICK blerrg");
                writer.Flush();
                writer.WriteLine("USER botty *: botty bot");
                writer.Flush();
                //writer.WriteLine("JOIN #tsam");
                //writer.Flush();

                data = new byte[tcpClient.ReceiveBufferSize];
                bytesRead = stream.Read(data, 0, System.Convert.ToInt32(tcpClient.ReceiveBufferSize));
                listener = new Thread(loop);
                listener.Start();

            }
            catch (Exception e)
            {
                readText += "Unable to connect : "+ e.Message;
            }

        }
        public bool isConnected()
        {
            if (tcpClient != null)
                return tcpClient.Connected;
            else
                return false;
        }
        //Delecate fall sem er notað svo það er hægt að kalla á önnur föll í þráðum
        //public delegate void UpdateTextCallback(string message);

        //loopa sem inn straum á socketinu
        private void loop()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(2);
                    if ((incomingText = reader.ReadLine()) != null)
                    {
                        if (incomingText.StartsWith("PING"))
                        {
                            sendMessage("PONG");
                        }
                        else
                        {
                            /*
                            if (incomingText.Contains("353"))
                            {
                                //List<string> MyList = new List<string>();
                                //string[] parameters;
                                int index = incomingText.IndexOf(":");
                                string x = incomingText.Substring(index);
                                Debug.WriteLine(x);
                                //char[] splitchar = { ' ' };
                                //parameters = incomingText.Split(splitchar);
                                //client.sendMessage("JOIN " + parameters[1]);
                                //listBox1.DataSource = MyList;
                            }*/
                            Debug.WriteLine(incomingText);
                            readText += incomingText;
                            if (incomingText.Contains("QUIT"))
                            {
                                listener.Abort();
                                tcpClient.Close();
                                
                            }
                            
                        }
                    }
                }
                catch(Exception e)
                {
                    Debug.WriteLine("Thread stopped");
                    //listener.Abort();
                }
            }
        }

        //Skilar textanum sem hefur verið lesinn inn
        public int getText(ref string text)
        {
            text = readText;
            readText = "";
            return text.Length;
        }

        public void sendMessage(string message)
        {
            writer.WriteLine(message);
            writer.Flush();
           
        }

    }
}
